package com.itheima.backstage.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.itheima.autoconfig.oss.OssTemplate;
import com.itheima.domain.db.QuestionUserLock;
import com.itheima.domain.db.Questionnaire;
import com.itheima.domain.db.SoulQuestion;
import com.itheima.domain.db.SoulQuestionOption;
import com.itheima.service.db.TestSoulService;
import com.itheima.vo.QuestionnaireVo;
import com.itheima.vo.SoulQuestionOptionVo;
import com.itheima.vo.SoulQuestionVo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
public class SoulSubjectManager {

    @Reference
    private TestSoulService testSoulService;

    //查询灵魂题目列表
    public List<QuestionnaireVo> findTestSoulList() {
        List<QuestionnaireVo> voList = new ArrayList<>();
        List<Questionnaire> questionnaireList = testSoulService.findQuestionnaireList();
        if (CollectionUtil.isNotEmpty(questionnaireList)) {
            for (Questionnaire questionnaire : questionnaireList) {
                QuestionnaireVo vo = new QuestionnaireVo();
                BeanUtil.copyProperties(questionnaire, vo);
                vo.setId(questionnaire.getId().toString());
                vo.setLevel(questionnaire.getLevel().toString());

                List<SoulQuestionVo> questionVoList = new ArrayList<>();
                List<SoulQuestion> questionList = testSoulService.findSoulQuestionListByQuestionnaireId(questionnaire.getId());
                if (CollectionUtil.isNotEmpty(questionList)) {
                    for (SoulQuestion soulQuestion : questionList) {
                        SoulQuestionVo questionVo = new SoulQuestionVo();
                        questionVo.setId(soulQuestion.getId().toString());
                        questionVo.setQuestion(soulQuestion.getQuestion());

                        List<SoulQuestionOptionVo> optionVoList = new ArrayList<>();
                        List<SoulQuestionOption> optionList = testSoulService.findSoulQuestionOptionListByQuestionId(soulQuestion.getId());
                        if (CollectionUtil.isNotEmpty(optionList)) {
                            for (SoulQuestionOption soulQuestionOption : optionList) {
                                SoulQuestionOptionVo optionVo = new SoulQuestionOptionVo();
                                optionVo.setId(soulQuestionOption.getId().toString());
                                optionVo.setOption(soulQuestionOption.getContent());

                                optionVoList.add(optionVo);
                            }
                        }

                        questionVo.setOptions(optionVoList);

                        questionVoList.add(questionVo);
                    }
                }

                vo.setQuestions(questionVoList);

                QuestionUserLock questionUserLock = testSoulService.findQuestionUserLockByQuestionnaireId(questionnaire.getId());
                vo.setIsLock(questionUserLock.getIsLock());

                voList.add(vo);
            }
        }

        return voList;
    }

    //添加或修改试卷
    public void saveTestSoul(QuestionnaireVo questionnaireVo) {
        Questionnaire questionnaire = new Questionnaire();

        BeanUtil.copyProperties(questionnaireVo, questionnaire);
        questionnaire.setLevel(Integer.parseInt(questionnaireVo.getLevel()));

        testSoulService.saveOrUpdateQuestionnaire(questionnaire);

        List<SoulQuestionVo> questionVoList = questionnaireVo.getQuestions();
        if (CollectionUtil.isNotEmpty(questionVoList)) {
            for (SoulQuestionVo questionVo : questionVoList) {
                SoulQuestion question = new SoulQuestion();
                question.setQuestionnaireId(Long.valueOf(questionnaireVo.getId()));
                question.setId(Long.valueOf(questionVo.getId()));
                question.setQuestion(questionVo.getQuestion());

                testSoulService.saveOrUpdateSoulQuestion(question);

                List<SoulQuestionOptionVo> optionVoList = questionVo.getOptions();
                if (CollectionUtil.isNotEmpty(optionVoList)) {
                    for (SoulQuestionOptionVo optionVo : optionVoList) {
                        SoulQuestionOption option = new SoulQuestionOption();
                        option.setQuestionId(Long.valueOf(questionVo.getId()));
                        option.setId(Long.valueOf(optionVo.getId()));
                        option.setContent(optionVo.getOption());
                        option.setScore("6");//给一个默认值

                        testSoulService.saveOrUpdateSoulQuestionOption(option);
                    }
                }
            }
        }
    }


    //根据问卷id查询问卷信息
    public QuestionnaireVo findTestSoulById(Long id) {
        QuestionnaireVo vo = new QuestionnaireVo();
        Questionnaire questionnaire = testSoulService.findQuestionnaireById(id);
        BeanUtil.copyProperties(questionnaire, vo);
        vo.setId(questionnaire.getId().toString());
        vo.setLevel(questionnaire.getLevel().toString());

        List<SoulQuestionVo> questionVoList = new ArrayList<>();
        List<SoulQuestion> questionList = testSoulService.findSoulQuestionListByQuestionnaireId(questionnaire.getId());
        if (CollectionUtil.isNotEmpty(questionList)) {
            for (SoulQuestion soulQuestion : questionList) {
                SoulQuestionVo questionVo = new SoulQuestionVo();
                questionVo.setId(soulQuestion.getId().toString());
                questionVo.setQuestion(soulQuestion.getQuestion());

                List<SoulQuestionOptionVo> optionVoList = new ArrayList<>();
                List<SoulQuestionOption> optionList = testSoulService.findSoulQuestionOptionListByQuestionId(soulQuestion.getId());
                if (CollectionUtil.isNotEmpty(optionList)) {
                    for (SoulQuestionOption soulQuestionOption : optionList) {
                        SoulQuestionOptionVo optionVo = new SoulQuestionOptionVo();
                        optionVo.setId(soulQuestionOption.getId().toString());
                        optionVo.setOption(soulQuestionOption.getContent());

                        optionVoList.add(optionVo);
                    }
                }

                questionVo.setOptions(optionVoList);

                questionVoList.add(questionVo);
            }
        }

        vo.setQuestions(questionVoList);

        QuestionUserLock questionUserLock = testSoulService.findQuestionUserLockByQuestionnaireId(questionnaire.getId());
        vo.setIsLock(questionUserLock.getIsLock());

        return vo;
    }

    //根据问卷id查询问题和选项
    public List<SoulQuestionVo> findSoulQuestionVoList(Long id) {
        List<SoulQuestionVo> questionVoList = new ArrayList<>();
        List<SoulQuestion> questionList = testSoulService.findSoulQuestionListByQuestionnaireId(id);
        if (CollectionUtil.isNotEmpty(questionList)) {
            for (SoulQuestion soulQuestion : questionList) {
                SoulQuestionVo questionVo = new SoulQuestionVo();
                questionVo.setId(soulQuestion.getId().toString());
                questionVo.setQuestion(soulQuestion.getQuestion());

                List<SoulQuestionOptionVo> optionVoList = new ArrayList<>();
                List<SoulQuestionOption> optionList = testSoulService.findSoulQuestionOptionListByQuestionId(soulQuestion.getId());
                if (CollectionUtil.isNotEmpty(optionList)) {
                    for (SoulQuestionOption soulQuestionOption : optionList) {
                        SoulQuestionOptionVo optionVo = new SoulQuestionOptionVo();
                        optionVo.setId(soulQuestionOption.getId().toString());
                        optionVo.setOption(soulQuestionOption.getContent());

                        optionVoList.add(optionVo);
                    }
                }
                questionVo.setOptions(optionVoList);
                questionVoList.add(questionVo);
            }
        }
        return questionVoList;
    }

    @Autowired
    private OssTemplate ossTemplate;

    //文件上传
    public String upload(MultipartFile file) throws IOException {
        String upload = null;
        if (file != null && file.getSize() > 0) {
           upload = ossTemplate.upload(file.getOriginalFilename(), file.getInputStream());
        }
        return "https://" + upload;
    }

    //保存题目
    public void saveTestSoulQuestion(Map map) {
        String questionnaireId = map.get("questionnaireId").toString();
        String question = map.get("question").toString();

        SoulQuestion soulQuestion = new SoulQuestion();
        soulQuestion.setQuestion(question);
        soulQuestion.setQuestionnaireId(Long.valueOf(questionnaireId));
        soulQuestion.setCreated(new Date());

        testSoulService.saveSoulQuestion(soulQuestion);
    }

    //保存选项
    public void saveTestSoulQuestionOption(Map map) {
        String questionId = map.get("questionId").toString();
        String option = map.get("option").toString();
        String score = map.get("score").toString();

        SoulQuestionOption soulQuestionOption = new SoulQuestionOption();
        soulQuestionOption.setQuestionId(Long.valueOf(questionId));
        soulQuestionOption.setContent(option);
        soulQuestionOption.setCreated(new Date());
        soulQuestionOption.setScore(score);

        testSoulService.saveSoulQuestionOption(soulQuestionOption);
    }

    //保存问卷
    public void saveQuestionnaire(Map map) {
        String name = map.get("name").toString();
        String cover = map.get("cover").toString();
        String level = map.get("level").toString();
        String star = map.get("star").toString();

        Questionnaire questionnaire = new Questionnaire();
        questionnaire.setName(name);
        questionnaire.setCover(cover);
        questionnaire.setLevel(Integer.valueOf(level));
        questionnaire.setStar(Integer.valueOf(star));
        questionnaire.setCreated(new Date());

        Long id = testSoulService.saveQuestionnaire(questionnaire);

        QuestionUserLock questionUserLock = new QuestionUserLock();
        questionUserLock.setQuestionnaireId(id);
        questionUserLock.setIsLock(0);
        questionUserLock.setCreated(new Date());

        testSoulService.saveQuestionUserLock(questionUserLock);
    }

    //根据问题id删除问题
    public void deleteByQuestionId(Long questionId) {
        List<SoulQuestionOption> optionList = testSoulService.findSoulQuestionOptionListByQuestionId(questionId);
        for (SoulQuestionOption soulQuestionOption : optionList) {
            testSoulService.deleteOptionById(soulQuestionOption.getId());
        }

        //删除问题
        testSoulService.deleteQuestion(questionId);
    }

    //根据问卷id删除问卷
    public void deleteByQuestionnaireId(Long questionnaireId) {
        List<SoulQuestion> soulQuestionList = testSoulService.findSoulQuestionListByQuestionnaireId(questionnaireId);
        if (CollectionUtil.isNotEmpty(soulQuestionList)) {
            for (SoulQuestion soulQuestion : soulQuestionList) {
                deleteByQuestionId(soulQuestion.getId());
            }
        }

        if (questionnaireId != null) {
            testSoulService.deleteByQuestionnaireId(questionnaireId);
        }

    }
}


